<template>
  <div class="login">
    <div class="login_box">
      <div class="box_logo">
        <img src="@/assets/logo.png" alt="">
      </div>
      <!-- form表单 -->
      <!-- 如果不需要label则取消label-width参数的设置，否则就会出现表单前留有空白的情况 -->
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" class="demo-ruleForm" >
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user-solid" v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="el-icon-warning" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item class="form_button"> 
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import addLogin from '@/utils/loginApi'
import { mapMutations } from 'vuex';
export default {
  data() {
    return {
      ruleForm: {
        username: '',
        password:''
      },
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 6 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    ...mapMutations(['addToken']),
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          this.$message.success('表单校验通过');
          /* 请求登录接口 */
          let res = await addLogin(this.ruleForm);
          if(res.meta.status == 200) {
            this.$message.success(res.meta.msg);
            this.addToken({token:res.data.token});
            this.$router.push('/');
          } else {
            this.$message.error(res.meta.msg);
          }
        } else {
          this.$message.success('表单校验通过');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>


<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  min-width: 720px;
  position: relative;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: white;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);

    .box_logo {
      width: 150px;
      height: 150px;
      border: 14px solid white;
      background-color: #ccc;
      border-radius: 50%;
      box-shadow: 0 0 10px black;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);

      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .demo-ruleForm {
      margin-top: 100px;
      padding: 0 20px;
      .form_button {
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
</style>